package cn.lls.service.impl;

import cn.lls.base.exception.LlsException;
import cn.lls.base.result.ResultCode;
import cn.lls.base.util.MD5Util;
import cn.lls.dao.EmployeeDao;
import cn.lls.dto.EmployeeLoginDTO;
import cn.lls.entity.company.EmployeeAccountEntity;
import cn.lls.entity.sys.EmployeeBaseEntity;
import cn.lls.entity.sys.EmployeeRoleEntity;
import cn.lls.req.emp.ConditionSearchEmployeeExcelReq;
import cn.lls.req.emp.ConditionSearchEmployeeReq;
import cn.lls.req.emp.EMPAccountReq;
import cn.lls.req.emp.EmployeeInfoReq;
import cn.lls.service.IEmployeeAccountService;
import cn.lls.service.IEmployeeBaseService;
import cn.lls.service.IEmployeeRoleService;
import cn.lls.service.IEmployeeService;
import cn.lls.vo.company.CompanyAllInfo;
import cn.lls.vo.emp.EmployeeInfo;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 员工信息操作服务类
 *
 * @ClassName EmployeeServiceImpl
 * @Author 划水艺术家
 * @Date 2022/11/21 23:39
 * @Version 1.0
 **/

@Service
@Slf4j
public class EmployeeServiceImpl extends ServiceImpl<EmployeeDao, EmployeeInfoReq> implements IEmployeeService {

    @Autowired
    private IEmployeeBaseService employeeBaseService;

    @Autowired
    private IEmployeeAccountService employeeAccountService;

    @Autowired
    private IEmployeeRoleService employeeRoleService;

    @Override
    public EmployeeInfo getEmployeeById(Long employeeId){
        return this.baseMapper.getEmployeeById(employeeId);
    }


    @Override
    public void updateEmployee(EmployeeInfoReq employeeInfoReq) {
        try {
            if (employeeInfoReq.getUserId() == null)
                throw new LlsException(ResultCode.DATA_ERROR);
            /*
            更改员工基本信息
            */
            LambdaUpdateWrapper<EmployeeBaseEntity> employeeBaseEntityLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
            employeeBaseEntityLambdaUpdateWrapper.eq(EmployeeBaseEntity::getUserId, employeeInfoReq.getUserId());
            EmployeeBaseEntity employeeBaseEntity = new EmployeeBaseEntity();
            BeanUtils.copyProperties(employeeInfoReq, employeeBaseEntity);
//            employeeBaseEntity.setJoinDate(employeeInfoReq.getJoinDate());

            log.info("empBaseEntity = {}", employeeBaseEntity);
            employeeBaseService.update(employeeBaseEntity, employeeBaseEntityLambdaUpdateWrapper);
            log.info("base over");
            /*
            更改员工角色信息
             */
            LambdaUpdateWrapper<EmployeeRoleEntity> employeeRoleEntityLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
            employeeRoleEntityLambdaUpdateWrapper.eq(EmployeeRoleEntity::getUserId, employeeInfoReq.getUserId());
            EmployeeRoleEntity employeeRoleEntity = new EmployeeRoleEntity();
            BeanUtils.copyProperties(employeeInfoReq, employeeRoleEntity);
            employeeRoleService.update(employeeRoleEntity, employeeRoleEntityLambdaUpdateWrapper);
            log.info("role over");
            /*
             更改员工账号信息（仅更改账号）
             */
            LambdaUpdateWrapper<EmployeeAccountEntity> employeeAccountEntityLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
            employeeAccountEntityLambdaUpdateWrapper.eq(EmployeeAccountEntity::getUserId, employeeInfoReq.getUserId());
            EmployeeAccountEntity employeeAccountEntity = new EmployeeAccountEntity();
            BeanUtils.copyProperties(employeeInfoReq, employeeAccountEntity);
            employeeAccountService.update(employeeAccountEntity, employeeAccountEntityLambdaUpdateWrapper);
        } catch (LlsException e){
            throw e;
        } catch (Exception e) {
            e.printStackTrace();
            throw new LlsException(ResultCode.DATABASE_ERROR, "数据更改失败");
        }
    }

    /*
   分页、条件查询员工信息
    */
    @Override
    public IPage<EmployeeInfo> getEmployeeByConditionAndPage(ConditionSearchEmployeeReq conditionSearchEmployeeReq) {
        IPage<CompanyAllInfo> page = new Page<>(conditionSearchEmployeeReq.getCurrentPage(), conditionSearchEmployeeReq.getPageSize());
        return this.baseMapper.getEmployeeInfoByPageAndCondition(page, conditionSearchEmployeeReq);
    }

    @Override
    public List<EmployeeInfo> getEmployeeExcel(ConditionSearchEmployeeExcelReq conditionSearchEmployeeExcelReq) {
        return this.baseMapper.getEmployeeExcel(conditionSearchEmployeeExcelReq);
    }

    @Override
    public EmployeeLoginDTO empLogin(EMPAccountReq req) {
        return this.baseMapper.empLogin(req);
    }

    @Override
    public void addEmployee(EmployeeInfoReq employeeInfoReq) {
        try {
            /*
            保存员工基本信息
            */
            EmployeeBaseEntity employeeBaseEntity = new EmployeeBaseEntity();
            BeanUtils.copyProperties(employeeInfoReq, employeeBaseEntity);
            employeeBaseService.save(employeeBaseEntity);
            /*
            保存员工角色信息
             */
            EmployeeRoleEntity employeeRoleEntity = new EmployeeRoleEntity();
            BeanUtils.copyProperties(employeeInfoReq, employeeRoleEntity);
            employeeRoleEntity.setUserId(employeeBaseEntity.getUserId());
            employeeRoleService.save(employeeRoleEntity);
            /*
             保存员工账号信息
             */
            EmployeeAccountEntity employeeAccountEntity = new EmployeeAccountEntity();
            BeanUtils.copyProperties(employeeInfoReq, employeeAccountEntity);
            employeeAccountEntity.setUserId(employeeBaseEntity.getUserId());
            // 设置账号为手机号
            employeeAccountEntity.setAccount(employeeBaseEntity.getEmployeeTel());
            // 设置密码为手机号
            employeeAccountEntity.setLoginPassword(MD5Util.MD5Encode(employeeBaseEntity.getEmployeeTel()));
            employeeAccountService.save(employeeAccountEntity);
        } catch (Exception e) {
            throw new LlsException(ResultCode.DATABASE_ERROR, "数据插入失败");
        }
    }
}
